<template>
	<view style="height: 100%;">
		<u-navbar title="商品列表" :autoBack="true" :bgColor="them_color" :titleStyle="{color:'#ffffff'}"
			leftIconColor="#ffffff"></u-navbar>
		<view class='productList' :style="'margin-top:' + navH + 'px;'">
			<view class='search bg-color acea-row row-between-wrapper'>
				<view class='input acea-row row-between-wrapper'><text class='iconfont icon-sousuo'></text>
					<input placeholder='搜索商品名称' placeholder-class='placeholder' confirm-type='search' name="search"
						:value='where.goods_name' @confirm="searchSubmit"></input>
				</view>
				<view class='iconfont' :class='is_switch==true?"icon-pailie":"icon-tupianpailie"' @click='Changswitch'>
				</view>
			</view>
			<view class='nav acea-row row-middle'>
				<view style="width: 25%;padding-left: 10px;" :class='title ? "font-num":""' @click='set_where(1)'>
					{{title ? title : '默认'}}
				</view>
				<view class='item' @click='set_where(2)'>
					价格
					<image v-if="shop_price==1" :src="ima_url+'/statics/yilangmp/up.png'"></image>
					<image v-else-if="shop_price==2"  :src="ima_url+'/statics/yilangmp/down.png'"></image>
					<image v-else :src="ima_url+'/statics/yilangmp/horn.png'"></image>
				</view>
				<view class='item' @click='set_where(3)'>
					销量
					<image v-if="click_count ==1"  :src="ima_url+'/statics/yilangmp/up.png'"></image>
					<image v-else-if="click_count ==2"  :src="ima_url+'/statics/yilangmp/down.png'"></image>
					<image v-else :src="ima_url+'/statics/yilangmp/horn.png'"></image>
				</view>
				<!-- down -->
				<view class='item' :class='nows ? "font-color":""' @click='set_where(4)'>新品</view>
			</view>
			<view class='list' :class='is_switch==true?"":"on"'>
				<view class='item' :class='is_switch==true?"":"on"' hover-class='none'
					v-for="(item,index) in productList" :key="index" @click="godDetail(item)">
					<image :src='item.original_img' style="width: 100%;" mode="widthFix"></image>
					<view class='text' :class='is_switch==true?"":"on"'>
						<view class='name line1'>{{item.goods_name}}</view>
						<view class='money font-color' :class='is_switch==true?"":"on"'>￥<text
								class='num'>{{item.shop_price}}</text></view>
						<view class='vip acea-row row-between-wrapper' :class='is_switch==true?"":"on"'>
							<view class='vip-money' v-if="item.vip_price && item.vip_price > 0">
								VIP ￥{{item.vip_price}}
								<!-- <image src='@/static/images/vip.png'></image> -->
							</view>
							<view>已售 {{item.goods_number}}{{item.unit_list[0].unit_name}}</view>
						</view>
					</view>
				</view>

			</view>
			<view class='loadingicon acea-row row-center-wrapper' v-if='productList.length > 0'>
				<text class='loading iconfont icon-jiazai' :hidden='loading==false'></text>{{loadTitle}}
			</view>
		</view>
		<view class='noCommodity' v-if="productList.length==0 && where.page > 1">
			<view class='emptyBox'>
				<u-empty mode="data" icon="http://cdn.uviewui.com/uview/empty/data.png">
				</u-empty>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		promote_list,
		plat_goods_list
	} from '@/api/store';
	import {
		mapGetters
	} from "vuex";
	import {
		BASE_URL
	} from '@/config/app';
	export default {
		components: {},
		computed: mapGetters(["userLocation", "area_id","loaded_area_id"]),
		data() {
			return {
				navH: '',
				them_color: getApp().globalData.theme_color,
				productList: [],
				is_switch: true,
				where: {
					cat_id: '',
					goods_name: '',
					page: 1,
					limit: 10,
					supplier_id: '',
					brand_id: '',
					order: '',
					sort: '',
					market_type: 1,
					is_snew: 0,
					is_sbest: 0,
					is_shot: 0,
					area_id:''
				},
				shop_price: 0,
				click_count: 0,
				nows: false,
				loadend: false,
				loading: false,
				loadTitle: '加载更多',
				title: '',
				hostProduct: [],
				hotPage: 1,
				hotLimit: 10,
				hotScroll: false,
				ima_url: BASE_URL
			}
		},
		onLoad(options) {
			this.navH = getApp().globalData.navHeight / 2 - 5;
			this.$set(this.where, 'cat_id', options.cat_id || 0);
			this.title = options.title || '';
			this.$set(this.where, 'goods_name', options.searchValue || '');
			this.$set(this.where, 'supplier_id', options.supplier_id || '');
			this.$set(this.where, 'brand_id', options.brand_id || '');
			this.$set(this.where, 'area_id', this.loaded_area_id);
			this.get_product_list()
		},
		methods: {
			// 去详情页
			godDetail(item) {
				uni.navigateTo({
					url: `/pages/goods/detail/index?goods_id=${item.goods_id}`
				})
			},
			Changswitch: function() {
				let that = this;
				that.is_switch = !that.is_switch
			},
			searchSubmit: function(e) {
				let that = this;
				that.$set(that.where, 'goods_name', e.detail.value);
				that.loadend = false;
				that.$set(that.where, 'page', 1)
				this.get_product_list(true);
			},
			/**
			 * 获取我的推荐
			 */
			get_host_product: function() {
				let that = this;
				if (that.hotScroll) return
				promote_list({
					page: that.hotPage
				}).then(res => {
					that.hotPage++
					that.hotScroll = res.data.length < that.hotLimit
					that.hostProduct = that.hostProduct.concat(res.data)
				});
			},
			//点击事件处理
			set_where: function(e) {
				switch (e) {
					case 1:
						break;
					case 2:
						if (this.shop_price == 0) this.shop_price = 1;
						else if (this.shop_price == 1) this.shop_price = 2;
						else if (this.shop_price == 2) this.shop_price = 0;
						this.click_count = 0;
						break;
					case 3:
						if (this.click_count == 0) this.click_count = 1;
						else if (this.click_count == 1) this.click_count = 2;
						else if (this.click_count == 2) this.click_count = 0;
						this.shop_price = 0
						break;
					case 4:
						this.nows = !this.nows;
						break;
				}
				this.loadend = false;
				this.$set(this.where, 'page', 1);
				this.get_product_list(true);
			},
			//设置where条件
			setWhere: function() {
				if (this.shop_price == 0 && this.click_count == 0) {
					this.where.order = '';
				} else if (this.shop_price == 1 && this.click_count == 0) {
					this.where.order = 'shop_price';
					this.where.sort = 'asc';
				} else if (this.shop_price == 2 && this.click_count == 0) {
					this.where.order = 'shop_price';
					this.where.sort = 'desc';
				} else if (this.click_count == 1 && this.shop_price == 0) {
					this.where.order = 'click_count ';
					this.where.sort = 'asc';
				} else if (this.click_count == 2 && this.shop_price == 0) {
					this.where.order = 'click_count ';
					this.where.sort = 'desc';
				}
				this.where.is_snew = this.nows ? 1 : 0;
			},
			//查找产品
			get_product_list: function(isPage) {
				let that = this;
				that.setWhere();
				if (that.loadend) return;
				if (that.loading) return;
				if (isPage === true) that.$set(that, 'productList', []);
				that.loading = true;
				that.loadTitle = '';

				plat_goods_list(that.where).then(res => {
					let list = res.data;
					let productList = that.$util.SplitArray(list, that.productList);
					let loadend = list.length < that.where.limit;
					that.loadend = loadend;
					that.loading = false;
					that.loadTitle = loadend ? '没有更多内容啦~' : '加载更多';
					that.$set(that, 'productList', productList);
					that.$set(that.where, 'page', that.where.page + 1);
					if (!that.productList.length) this.get_host_product();
				}).catch(err => {
					that.loading = false;
					that.loadTitle = '加载更多';
				});
			},
		},
		onPullDownRefresh() {

		},
		onReachBottom() {
			if (this.productList.length > 0) {
				this.get_product_list();
			} else {
				this.get_host_product();
			}

		}
	}
</script>

<style scoped lang="scss">
	.productList .search {
		width: 100%;
		height: 86rpx;
		padding-left: 23rpx;
		box-sizing: border-box;
		// position: fixed;
		// left: 0;
		// top: 0;
		z-index: 9;
	}

	.productList .search .input {
		width: 640rpx;
		height: 60rpx;
		background-color: #fff;
		border-radius: 50rpx;
		padding: 0 20rpx;
		box-sizing: border-box;
	}

	.productList .search .input input {
		width: 548rpx;
		height: 100%;
		font-size: 26rpx;
	}

	.productList .search .input .placeholder {
		color: #999;
	}

	.productList .search .input .iconfont {
		font-size: 35rpx;
		color: #555;
	}

	.productList .search .icon-pailie,
	.productList .search .icon-tupianpailie {
		color: #fff;
		width: 62rpx;
		font-size: 40rpx;
		height: 86rpx;
		line-height: 86rpx;
	}

	.productList .nav {
		height: 100rpx;
		color: #454545;
		// position: fixed;
		// left: 0;
		width: 100%;
		font-size: 28rpx;
		background-color: #fff;
		// margin-top: 86rpx;
		// top: 0;
		z-index: 9;
	}

	.productList .nav .item {
		width: 25%;
		text-align: center;
	}

	.productList .nav .item.font-color {
		font-weight: bold;
	}

	.productList .nav .item image {
		width: 15rpx;
		height: 19rpx;
		margin-left: 10rpx;
	}

	.productList .list {
		padding: 0 20rpx;
		margin-top: 20rpx;
		column-count: 2;
		column-gap: 10rpx;
	}

	.productList .list.on {
		background-color: #fff;
		border-top: 1px solid #f6f6f6;
	}

	.productList .list .item {
		border-radius: 8px;
		width: 335rpx;
		margin-bottom: 10px;
		background-color: #fff;
		-moz-page-break-inside: avoid;
		-webkit-column-break-inside: avoid;
		break-inside: avoid;
		overflow: hidden;
	}

	.productList .list .item.on {
		width: 100%;
		display: flex;
		border-bottom: 1rpx solid #f6f6f6;
		padding: 30rpx 0;
		margin: 0;
	}

	.productList .list .item .pictrue {
		position: relative;
		width: 100%;
	}

	.productList .list .item .pictrue.on {
		width: 180rpx;
	}

	.productList .list .item .pictrue image {
		width: 100%;
	}

	.productList .list .item .pictrue image.on {}

	.productList .list .item .text {
		padding: 20rpx 17rpx 26rpx 17rpx;
		font-size: 30rpx;
		color: #222;
	}

	.productList .list .item .text.on {
		width: 508rpx;
		padding: 0 0 0 22rpx;
	}

	.productList .list .item .text .money {
		font-size: 26rpx;
		font-weight: bold;
		margin-top: 8rpx;
		color: $theme-color;
	}

	.productList .list .item .text .money.on {
		margin-top: 50rpx;
	}

	.productList .list .item .text .money .num {
		font-size: 34rpx;
	}

	.productList .list .item .text .vip {
		font-size: 22rpx;
		color: #aaa;
		margin-top: 7rpx;
	}

	.productList .list .item .text .vip.on {
		margin-top: 12rpx;
	}

	.productList .list .item .text .vip .vip-money {
		font-size: 24rpx;
		color: #282828;
		font-weight: bold;
	}

	.productList .list .item .text .vip .vip-money image {
		width: 46rpx;
		height: 21rpx;
		margin-left: 4rpx;
	}

	.noCommodity {
		background-color: #fff;
		padding-bottom: 30rpx;

		.emptyBox {
			text-align: center;
			padding-top: 20rpx;

			.tips {
				color: #aaa;
				font-size: 26rpx;
			}

			image {
				width: 414rpx;
				height: 304rpx;
			}
		}
	}
</style>